<?php
namespace app\frontend\controller;
use think\Db;
use think\Controller;
class Shopcart extends Controller
{
    public function index()
    {
        if(!session('user_name'))
        {
            $user_name='登录';
            $user_register='注册';
            $user_logout='';
        }
        else
        {
        $user_name=session('user_name');
        $user_register='欢迎你';
        $user_logout='退出';
        }

        $this->assign('user_name',$user_name);
        $this->assign('user_register',$user_register);
        $this->assign('user_logout',$user_logout);
        $user_id=session('user_id');
        $list=db('shopcart')->alias('a')->join('product b','b.product_number=a.product_number','left')->where("a.user_id={$user_id} and status=0")->select();
        $user_score=db('user')->where("user_id='{$user_id}'")->field('user_score')->select();
     
        foreach ($user_score as $val) 
        { 
          $user_score=$val['user_score'];
        }

        

        if($user_score>=50 and $user_score<100)
        {
          $user_score='9.5折';

        } 
        else if ($user_score>=100 and $user_score<150) {
          $user_score='9折';
        }
        else if ($user_score>=150) {
          $user_score='8折';
        }
        else
        {
          $user_score='无';
        }
    	//$list=db('shopcart')->alias('a')->join('product b','b.product_number=a.product_number','left')->join('hotel c','c.product_number=a.product_number','left')->field('b.product_name as sname,c.product_name as hname,c.*')->where("a.user_id={$user_id}")->select();
        /*$data=Db::name('shopcart')->alias('c')
                    ->join('product b','c.product_number=b.product_number','LEFT')
                    ->join('hotel a','c.product_number=a.product_number','LEFT')
                    ->where(['c.user_id'=>$user_id])
                      ->field('a.product_price,b.product_price')
                    ->select();a.name as aname,b.name as uname,a.*'
        */


    	
        foreach ($list as $value) 
        {
            $shopcart_id[]=$value['shopcart_id'];
        	$product_name[]=$value['product_name'];
            $product_num[]=$value['product_number'];
            $product_price[]=$value['product_price'];
            $product_amount[]=$value['product_amount'];
            $product_detail[]=$value['product_detail'];
            
           
        }
        //dump($list);
       
        /*for($i=0;$i<count($product_price);$i++)
        {
        $total[$i]=$product_num[$i]*$product_price[$i];
        }
       
        $this->assign('total',$total);*/
    	$this->assign('list',$list);
        $this->assign('user_score',$user_score);

      
    	return view('./index/shopcart');
    }
    public function ajax()
    {
        $id=$_POST['id'];
    	$count=$_POST['count'];
        $priceTotal=$_POST['priceTotal'];
        $data=[
          'product_amount'=>$count,
          'product_subtotal'=>$priceTotal
          
         ];

        $ret=db('shopcart')->where("product_number={$id}")->update($data);

    	return $data;
    }
    public function delete()
    {
        $id=$_POST['id'];

       
        $ret=db('shopcart')->where("shopcart_id={$id}")->delete();

        return $id;
    }
}